<?php
class filedocument extends Controller{

	function index(){
		if(!$this->user->isLogged()){
			$this->response->redirect(MAIN_PAGE);
		}
		$datetime=explode(" ",date("Y-m-d H:i:s"));
		$this->data["stime"]=$datetime[1];
		$this->load->model("dept");
		$this->data["dept_list"]=$this->model_dept->loaddepartment()->rows;
		$this->document->setTitle("Document Management Solutions");
		$this->document->setSelectedMenu("menu_filedoc");
		/*
		$this->document->addLink("jqueryui/jquery.ui.all.css");
		$this->document->addLink("jquery.timepicker.css");
		$this->document->scripts=array("jquery-ui/jquery-ui-1.8.16.custom.min.js","jquery.timepicker.min.js");
		*/
		$this->document->scripts=array("date.format.js");
		$this->children=array("header","footer");
		$this->template="filedocument.html";
		$this->response->setOutput($this->render(TRUE),0);
	}

	function filedocumentpost(){
		$jsonarr=array();
		if($this->request->is_ajax_request() && $this->user->isLogged()){
			$sql="insert into filedocument(fileno,documentno,fileddate,filedtime,filedby,departmentid,user_id,active_yn)".
            " values('{$this->db->escape($_POST["fileno"])}','{$this->db->escape($_POST["docno"])}',{$this->convertdate($_POST["filingdate"])},".
            "'{$this->db->escape($_POST["filingtime"])}','{$this->db->escape($_POST["filedby"])}','{$this->db->escape($_POST["deptid"])}','{$this->user->getId()}',".
            "'Y')";
			$this->db->query($sql);
			$jsonarr["filedocumentid"]=$this->db->getLastId();
			$jsonarr["status"]=true;
			$jsonarr["msg"]="File document saved sucessfully";
		}
		else{
			$jsonarr["status"]=false;
			$jsonarr["redirect"]=true;
		}
		echo json_encode($jsonarr);
	}

	private function convertdate($d){
		return "STR_TO_DATE('{$d}','%m/%d/%Y')";
	}

}